package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.*;
import com.hghivln.pojo.domain.OrderBackfee;
import com.hghivln.pojo.domain.OrderBackfeeItem;
import com.hghivln.pojo.dto.OrderBackfeeDto;
import com.hghivln.pojo.dto.OrderBackfeeFormDto;
import com.hghivln.pojo.dto.OrderBackfeeItemDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.OrderBackfeeService;
import com.hghivln.service.OrderChargeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(methods = {
        @Method(name = "saveOrderAndItems", retries = 0, timeout = 6000),
        @Method(name = "backSuccess",retries = 0,timeout = 6000)
})
public class OrderBackfeeServiceImpl implements OrderBackfeeService {

    @Autowired
    private OrderBackfeeMapper orderBackfeeMapper;

    @Autowired
    private OrderBackfeeItemMapper orderBackfeeItemMapper;

    @Autowired
    private OrderChargeItemMapper orderChargeItemMapper;

    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Override
    public void saveOrderAndItems(OrderBackfeeFormDto orderBackfeeFormDto) {
        OrderBackfeeDto orderBackfeeDto = orderBackfeeFormDto.getOrderBackfeeDto();
        List<OrderBackfeeItemDto> orderBackfeeItemDtoList = orderBackfeeFormDto.getOrderBackfeeItemDtoList();
        OrderBackfee orderBackfee = new OrderBackfee();
        BeanUtils.copyProperties(orderBackfeeDto, orderBackfee);
        orderBackfee.setCreateBy(orderBackfeeDto.getSimpleUser().getUserName());
        orderBackfee.setCreateTime(DateUtil.date());
        orderBackfee.setBackStatus(Constants.ORDER_BACKFEE_STATUS_0);
        orderBackfeeMapper.insert(orderBackfee);

        for (OrderBackfeeItemDto orderBackfeeItemDto : orderBackfeeItemDtoList) {
            OrderBackfeeItem orderBackfeeItem = new OrderBackfeeItem();
            BeanUtils.copyProperties(orderBackfeeItemDto,orderBackfeeItem);
            orderBackfeeItem.setStatus(Constants.ORDER_BACKFEE_STATUS_0);
            orderBackfeeItem.setBackId(orderBackfee.getBackId());
            orderBackfeeItemMapper.insert(orderBackfeeItem);
        }

    }

    @Override
    public void backSuccess(String backId, String tradeNo, String backType) {

        // 更新退费订单状态
        OrderBackfee orderBackfee = orderBackfeeMapper.selectById(backId);
        orderBackfee.setBackStatus(Constants.ORDER_BACKFEE_STATUS_1);
        orderBackfee.setBackPlatformId(tradeNo);
        orderBackfee.setBackTime(DateUtil.date());
        orderBackfee.setBackType(backType);
        orderBackfeeMapper.updateById(orderBackfee);

        // 更新退费详情订单状态
        QueryWrapper<OrderBackfeeItem> qw=new QueryWrapper<>();
        qw.eq(OrderBackfeeItem.COL_BACK_ID,backId);
        List<OrderBackfeeItem> orderBackfeeItems = this.orderBackfeeItemMapper.selectList(qw);
        List<String> allItemIds=new ArrayList<>();
        for (OrderBackfeeItem orderBackfeeItem : orderBackfeeItems) {
            allItemIds.add(orderBackfeeItem.getItemId());
        }
        orderBackfeeItemMapper.updateOrderBackfeeItemByIds(allItemIds,Constants.ORDER_DETAILS_STATUS_2);

        // 更新支付订单详情状态
        orderChargeItemMapper.updateByItemIds(allItemIds,Constants.ORDER_DETAILS_STATUS_2);

        // 更新处方详情状态
        careOrderItemMapper.updateByItemIds(allItemIds,Constants.ORDER_DETAILS_STATUS_2);
    }

    @Override
    public AjaxResult queryAllOrderBackfeeForPage(OrderBackfeeDto orderBackfeeDto) {
        Page<OrderBackfee> page = new Page<>(orderBackfeeDto.getPageNum(), orderBackfeeDto.getPageSize());
        QueryWrapper<OrderBackfee> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(orderBackfeeDto.getPatientName()),OrderBackfee.COL_PATIENT_NAME,orderBackfeeDto.getPatientName());
        qw.like(StringUtils.isNotBlank(orderBackfeeDto.getRegId()),OrderBackfee.COL_REG_ID,orderBackfeeDto.getRegId());
        qw.orderByDesc(OrderBackfee.COL_CREATE_TIME);
        orderBackfeeMapper.selectPage(page,qw);
        return AjaxResult.success("操作成功",page.getRecords(),page.getTotal());
    }

    @Override
    public AjaxResult queryOrderBackfeeItemByBackId(String backId) {
        QueryWrapper<OrderBackfeeItem> qw=new QueryWrapper<>();
        qw.eq(OrderBackfeeItem.COL_BACK_ID, backId);
        return AjaxResult.success(this.orderBackfeeItemMapper.selectList(qw));
    }
}
